package com.o2o.factory;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.stereotype.Component;

import com.o2o.content.SessionContent;
import com.o2o.exception.CategoryNumberErrorException;
import com.o2o.model.topic.Category;
import com.o2o.service.UserService;
import com.sun.org.apache.bcel.internal.generic.NEW;

@Component
public class CategorySourceService {
	
	private static Map<Long,Category> catesMap = Collections.synchronizedMap(new HashMap<Long,Category>());
	
	public static Map<Long,List<Category>> pCatesMap = Collections.synchronizedMap(new TreeMap<Long, List<Category>>());//key为pid,value为自身的多对多Map，key可以有多个
	
	private static Map<Integer,Category> numberMap = Collections.synchronizedMap(new HashMap<Integer,Category>());//key为number
	
	@Resource
	UserService userService;
	
	private static CategorySourceService categorySourceService;
	
	public CategorySourceService(){
		
	}
	 @PostConstruct  //初始化之前执行
	 public void init(){
		categorySourceService = this;
		categorySourceService.userService = this.userService;
		List<Category> allCategorys = categorySourceService.userService.getAllCategorys(1);
		this.putAllCategory(allCategorys);
		System.out.println("all cates size="+catesMap.size());
	 }
	
	
	@SuppressWarnings("unchecked")
	public CategorySourceService(Map<String,Object> session){
//		if(catesMap == null || catesMap.size() == 0){ //如果catesMap为空会尝试加载
//			List<Category> allCategorys = categorySourceService.userService.getAllCategorys(1);
//			this.putAllCategory(allCategorys);
//			System.out.println("all cates size="+catesMap.size());
//		}
//		List<Category> cates = new ArrayList<Category>();
//		cates = (List<Category>) session.get(SessionContent.SESSION_CATEGORY_DOMTREE);
//		if(cates == null || cates.size() == 0){ //把一级导航放入session中
//			List<Category> categoryDomTree = this.getCategoryDomTree(this.getFirstGradeCategorys(1));
//			session.put(SessionContent.SESSION_CATEGORY_DOMTREE, categoryDomTree);
//		}
	}
	
	
	
	/**
	 * 获取一级分类
	 * @param type 分类的类型，1和3为普通商品分类，2是本地生活服务分类
	 * @return
	 */
	public List<Category> getFirstCategorys(int type){
		int pid = 1;
		if(type == 1 || type == 3){
			pid=1;
		}
		if(type == 2){
			pid=2;
		}
		List<Category> cates = new ArrayList<Category>();
		for(Category cate:catesMap.values()){
			if(cate.getPid() == pid){
				cates.add(cate);
			}
		}
		return cates;
	}
	
	public Category getCategory(int number){
		return numberMap.get(number);
	}
	/**
	 * 递归获取全部类目Tree
	 * @param paramterCates 
	 * @return
	 */
	public List<Category> getCategoryDomTree(List<Category> paramterCates){
		List<Category> cates = new ArrayList<Category>();
		for(Category cate:paramterCates){
			List<Category> childCategory = this.getSubCategory(cate.getId());
			if(childCategory.size() > 0)
				cate.setSubCates(this.getCategoryDomTree(childCategory));
			cates.add(cate);
		}
		return cates;
	}
	
	
	
	public Category getCategory(long cateId){
		return catesMap.get(cateId);
	}
	
	public void removeCategory(long cateId){
		catesMap.remove(cateId);
	}
	/**
	 * 得到父类分类
	 * @param cid 本身id
	 * @return 父类Category
	 */
	public Category getParentCategory(long cid){
		for(Category c : catesMap.values()){
			if(c.getId() ==  cid)
				return c;
		}
		return null;
	}
	/**
	 * 得到子类分类
	 * @param cid 本身id
	 * @return List<Category>
	 */
	public List<Category> getSubCategory(long cid){
		List<Category> cates = new ArrayList<Category>();
		for(Category c : catesMap.values()){
			if(c.getPid() ==  cid)
				cates.add(c);
		}
		return cates;
	}
	
	public List<Category> getAllCategory(){
		List<Category> cates = new ArrayList<Category>();
		for(Category c : catesMap.values()){
			cates.add(c);
		}
		return cates;
	}
	
	
	public void putAllCategory(List<Category> cates){
		if(cates != null){
			for(Category cate : cates){
				List<Category> list = pCatesMap.get(cate.getPid());
				if(list == null){
					list = new ArrayList<Category>();
					list.add(cate);
					pCatesMap.put(cate.getPid(), list);
				}else{
					list.add(cate);
				}
				catesMap.put(cate.getId(), cate);
				numberMap.put(cate.getCategoryNumber(), cate);
			}
		}
	}
	
	/**
	 * 根据categoryNumber获取该number的一级分类
	 * @param categoryNumber 该分类的number
	 * @return 一级分类Category
	 * @throws CategoryNumberErrorException 分类number错误
	 */
	public Category getFirstCategoryByNumber(int categoryNumber,int status) throws CategoryNumberErrorException {
		int first = categoryNumber / 1000000;
		if(first > 0 && first <10){
			int number =  categoryNumber/1000000*1000000;
			return numberMap.get(number);
		}else throw new CategoryNumberErrorException("CategoryNumber Error,Frist category number must > 0; x,xx,xx,xx");
	}
	
	public List<Category> getCategoryListByPid(long pid){
		return pCatesMap.get(pid);
	}
	/**
	 * 根据categoryNumber获取该number的二级分类
	 * @param categoryNumber 该分类的number
	 * @return 二级分类Category
	 * @throws CategoryNumberErrorException 分类number错误
	 */
	public Category getSecondCategoryByNumber(int categoryNumber,int status) throws CategoryNumberErrorException {
		int first, second;
		first = categoryNumber / 1000000;
		second = (categoryNumber % 1000000) / 10000;
		if (first > 0 && second > 0) {// 该categoryNumber含有二级分类
			int number = categoryNumber / 10000 * 10000;
			return numberMap.get(number);
		} else throw new CategoryNumberErrorException("CategoryNumber Error,Second category number must >0;x,xx,xx,xx");
	}
	/**
	 * 根据categoryNumber获取该number的三级分类
	 * @param categoryNumber 该分类的number
	 * @return 三级分类Category
	 * @throws CategoryNumberErrorException 分类number错误
	 */
	public Category getThirdCategoryByNumber(int categoryNumber,int status) throws CategoryNumberErrorException {
		int first, second, third;
		first = categoryNumber / 1000000;
		second = (categoryNumber % 1000000) / 10000;
		third = (categoryNumber % 10000) / 100;
		if (first > 0 && second > 0 && third > 0) {// 该分类有三级分类
			int number = categoryNumber / 100 * 100;
			return numberMap.get(number);
		} else throw new CategoryNumberErrorException("CategoryNumber Error,Third category number must >0;x,xx,xx,xx");
	}
	
	/**
	 * 根据categoryNumber获取该number的四级分类
	 * @param categoryNumber 该分类的number
	 * @return 四级分类Category
	 * @throws CategoryNumberErrorException 分类number错误
	 */
	public Category getFourthCategoryByNumber(int categoryNumber,int status) throws CategoryNumberErrorException {
		int first, second, third, fourth;
		first = categoryNumber / 1000000;
		second = (categoryNumber % 1000000) / 10000;
		third = (categoryNumber % 10000) / 100;
		fourth = categoryNumber % 100;
		if (first > 0 && second > 0 && third > 0 && fourth > 0) {// 该分类有四级分类
			return numberMap.get(categoryNumber);
		} else throw new CategoryNumberErrorException("CategoryNumber Error,Fourth category number must >0;x,xx,xx,xx");
	}
}
